package com.jp.knowledge.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.JpApplication;
import com.jp.knowledge.model.FoundAppData;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.L;

import java.util.List;

/**
 * Created by jiayen on 16/11/15.
 */

public class FoundAdapter extends  JpBaseRecyViewAdapter {
    private JpApplication  application;
    public FoundAdapter(Context context, List data) {
        super(context, data);
        application = (JpApplication) ((Activity)context).getApplication();
    }

    @Override
    public int getLayoutId() {
        return R.layout.com_text_layout_found;
    }

    @Override
    public void bindViewHold(JPRecycleViewHolder holder, int position) {
        ImageView icon = holder.getView(R.id.icon);
        TextView  name = holder.getView(R.id.name);
        FoundAppData appData = (FoundAppData) data.get(position);
        ImageLoadUtil.load(mContext,appData.getIcon(),icon);
        name.setText(appData.getName());

//        if(appData.getActivation()==1){
//            setCommView(icon,name,appData,false);
//        }else {
//            int permission = (int) appData.getPermission();
//            if(permission==1){//用户限定
//                    if(application.isLogin()){//有登录
//                        setCommView(icon,name,appData,false);
//                    }else{//没有登录
//                        setCommView(icon,name,appData,true);
//                    }
//            }else if(permission==2){//指定限定
//                    int viewPermit = (int) appData.getViewPermit();
//                    if(viewPermit==1){//要求成为vip会员
//                        try {
//                            if(application.getUserData().getIsVip()==1){
//                                setCommView(icon,name,appData,false);
//                            }else{
//                                setCommView(icon,name,appData,true);
//                            }
//                        }catch (Exception e){
//                            L.e(e.getMessage());
//                        }
//
//                    }else if(viewPermit==2){//要求成为企业会员
//                        setCommView(icon,name,appData,true);
//                    }
//            }
//            icon.setBackground(getBg("#969696"));
//            name.setTextColor(mContext.getResources().getColor(R.color.font_gray));
//        }

        setCommView(icon, name, appData, appData.getActivation() != 1);
    }

    private void setCommView( ImageView icon, TextView  name,FoundAppData appData,boolean gray){
        if(gray){
            icon.setBackground(getBg(mContext.getResources().getColor(R.color.font_gray_think)));
            name.setTextColor(mContext.getResources().getColor(R.color.font_gray_light));
        }else {
            icon.setBackground(getBg(appData.getColor()));
            name.setTextColor(mContext.getResources().getColor(R.color.font_black_think));
        }
    }


    private GradientDrawable getBg(String color){
        try {
            return getBg(Color.parseColor(color));
        } catch (Exception e){
            return null;
        }
    }

    private GradientDrawable getBg(int color){
        try {
            GradientDrawable gd = new GradientDrawable();
            gd.setColor(color);
            gd.setCornerRadius(10);
            gd.setStroke(1, color);
            return gd;
        } catch (Exception e){
            return null;
        }
    }
}
